package cn.edu.seu.herald.informationplatform.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.edu.seu.herald.informationplatform.bean.league.Recruit;
import cn.edu.seu.herald.informationplatform.dao.cookie.LeagueUserSessionCookieAccess;
import cn.edu.seu.herald.informationplatform.dao.database.RecruitDao;
import cn.edu.seu.herald.informationplatform.util.filetools.PicTools;

public class RecruitDelete extends HttpServlet {
	
	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response)
					throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		try {
			LeagueUserSessionCookieAccess luSessionCookieAccess =
					new LeagueUserSessionCookieAccess(request);
			int leagueId = luSessionCookieAccess.getLeagueId();
			
			RecruitDao recruitAccess = RecruitDao.getDao();
			Recruit previousRecruit = recruitAccess.getRecruitById(leagueId);
			
			if (previousRecruit == null) {
				response.sendError(400);
				return;
			}
			String attachment = previousRecruit.getAttachmentAddress();
			PicTools.DeletePic(attachment);
			recruitAccess.deleteRecuitByLeagueId(leagueId);
			response.sendRedirect("./recruit.jsp?league="+luSessionCookieAccess.getLeagueId()); //定向的页面
			
		} catch (Exception e) {
			response.sendError(500);
			return;
		}
	}

}
